package io.cici.cc.mybatis.lite.binding;

import io.cici.cc.mybatis.lite.binding.MapperProxy.MapperMethodInvoker;
import io.cici.cc.mybatis.lite.session.Session;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author huihui
 */
public class MapperProxyFactory<T> {
    private final Class<T> mapperInterface;
    private final Map<Method, MapperMethodInvoker> methodToMapperMethodInvokerMap = new ConcurrentHashMap<>();

    public MapperProxyFactory(Class<T> mapperInterface) {
        this.mapperInterface = mapperInterface;
    }

    public T newInstance(Session session) {
        final MapperProxy<T> mapperProxy = new MapperProxy<>(mapperInterface, methodToMapperMethodInvokerMap, session);
        return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[]{mapperInterface}, mapperProxy);
    }
}
